﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI;
using Jungle.Code.Utils;
using Jungle.Code.Core;

using Jungle.Code.CategoryManager;

namespace Jungle.Code.CategoryManager.WebControls
{
    public class CategoriesList : WebControl, IPostBackEventHandler
    {
        int max_r = JungleTreeMenu.GetMaxRightKey();
        string REZULT = "";
        
        //------------------------------------------------------------------------------------------------------
        public void RaisePostBackEvent(string eventArgument)
        {
            int pos = eventArgument.IndexOf(',');
            int l = eventArgument.Length;
            string s1 = eventArgument.Substring(0, pos);
            string s2 = eventArgument.Substring(pos + 1, (l - (pos + 1)));

            
            if (s1 == "delCategory") 
            {
                JungleTreeMenu jMenu = new JungleTreeMenu();
                jMenu.ID = Convert.ToInt32(s2);
                jMenu.GetById();
                jMenu.Delete();
                Context.Response.Redirect("/admin/Modules.aspx?mod=Categories&cmd=view");                
            }
            if (s1 == "moveUp")
            {
                                               
                JungleTreeMenu jMover = new JungleTreeMenu();
                
                //string rez = jMover.ChangeOrder(id_mu, id_ml);

                string rez = jMover.PrepareForMove(Convert.ToInt32(s2), 0);

                Context.Response.Redirect("/admin/Modules.aspx?mod=Categories&cmd=view");
            }
            if (s1 == "moveDown")
            {
                                               
                JungleTreeMenu jMover = new JungleTreeMenu();
                
                //string rez = jMover.ChangeOrder(id_mu, id_ml);

                 string rez = jMover.PrepareForMove(Convert.ToInt32(s2), 1);

                Context.Response.Redirect("/admin/Modules.aspx?mod=Categories&cmd=view"); 
            }
            

        }
        //--------------------------------------------------------------------
        protected override void RenderContents(HtmlTextWriter writer)
        {

           #region Tree admin
            writer.AddAttribute("width", "90%");
            writer.AddAttribute("height", "90%");
            writer.AddAttribute(HtmlTextWriterAttribute.Cellpadding, "0");
            writer.AddAttribute(HtmlTextWriterAttribute.Cellspacing, "0");
            writer.RenderBeginTag(HtmlTextWriterTag.Table);


            writer.AddAttribute("bgcolor", "#CCCCCC");
            writer.RenderBeginTag(HtmlTextWriterTag.Tr); //<tr>
            writer.AddAttribute("width", "30px");
            writer.AddAttribute("class", "products_table_td");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.AddAttribute("class", "products_table_title");
            writer.RenderBeginTag(HtmlTextWriterTag.Font);
            writer.Write("ID");
            writer.RenderEndTag();
            writer.RenderEndTag();

            writer.AddAttribute("class", "products_table_td");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.AddAttribute("class", "products_table_title");
            writer.RenderBeginTag(HtmlTextWriterTag.Font);
            writer.Write("Название");
            writer.RenderEndTag();
            writer.RenderEndTag();
            
            writer.AddAttribute("class", "products_table_td");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.AddAttribute("class", "products_table_title");
            writer.RenderBeginTag(HtmlTextWriterTag.Font);
            writer.Write("Действия");
            writer.RenderEndTag();
            writer.RenderEndTag();

            writer.RenderEndTag(); //</tr>

            JungleTreeMenu jMenu = new JungleTreeMenu();

            jMenu.GetAll();
            string skew_Level = "";
            int count = 0;
            int MaxId = jMenu.MenuItemsList.Count;
            
            
            foreach (JungleTreeMenu jItem in jMenu.MenuItemsList) //шаг 2 получение одчерних категорий в виде сиска
            {
                
                if (jItem.LevelId == 2) { writer.AddAttribute("bgcolor", "#EEEEEE"); }
                writer.RenderBeginTag(HtmlTextWriterTag.Tr); //<tr>
                writer.AddAttribute("width", "30px");
                writer.AddAttribute("class", "products_table_td");
                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                writer.AddAttribute("class", "products_table_title");
                writer.RenderBeginTag(HtmlTextWriterTag.Font);
                writer.Write(jItem.ID.ToString());
                writer.RenderEndTag();
                writer.RenderEndTag();

                writer.AddAttribute("class", "products_table_td");
                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                writer.AddAttribute("class", "products_table_title");
                writer.RenderBeginTag(HtmlTextWriterTag.Font);
                skew_Level = "";
                for (int i = 1; i <= jItem.LevelId; i++) { skew_Level += "-&nbsp;"; }
                              
                writer.Write(skew_Level + " " + jItem.MenuText.Trim());
                
                writer.RenderEndTag();
                writer.RenderEndTag();

                writer.AddAttribute("class", "products_table_td");
                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                writer.AddAttribute("class", "products_table_title");
                writer.RenderBeginTag(HtmlTextWriterTag.Font);
                if (jItem.ID > 1)
                {
                    PrintBtnDel(writer, jItem.ID);
                    PrintBtnEdit(writer, jItem.ID);
                    PrintBtnUp(writer, jItem.ID);                   
                    PrintBtnDown(writer, jItem.ID);                        
                }
                writer.RenderEndTag();
                writer.RenderEndTag();

                writer.RenderEndTag();

                count++;
            }
        
            writer.RenderEndTag();    
            #endregion


            base.RenderContents(writer);
        }       

        //-----------------------------------------------------------------------------------------------
        public HtmlTextWriter PrintBtnDel(HtmlTextWriter writer, int catId)
        {
            ClientScriptManager cs = Page.ClientScript;
            PostBackOptions opt = new PostBackOptions(this);
            opt.ActionUrl = Context.Request.RawUrl;
            opt.Argument = "delCategory," + catId.ToString();

            writer.AddAttribute("onclick", "if(confirm(\"Вы действительно хотите удалить данную категорию?\"))" + cs.GetPostBackEventReference(opt));
            writer.AddAttribute("title", "Удалить");
            writer.AddAttribute("style", "cursor: pointer;");
            PrintImage(writer, "/Admin/img/btn_blue_delete.jpg");
            return writer;
        }
        //-----------------------------------------------------------------------------------------------
        public HtmlTextWriter PrintBtnEdit(HtmlTextWriter writer, int catId)
        {
            string linkStr = Context.Request.RawUrl;
            int t = linkStr.IndexOf("cmd");

            if (t != -1)
            {
                linkStr = linkStr.Substring(0, t + 4);
            }

            writer.AddAttribute(HtmlTextWriterAttribute.Href, linkStr + "edit&cId=" + catId.ToString());
            writer.RenderBeginTag(HtmlTextWriterTag.A);
            writer.AddAttribute("title", "Редактировать");
            writer.AddAttribute("style", "cursor: pointer;");
            PrintImage(writer, "/Admin/img/btn_blue_edit.jpg");
            writer.RenderEndTag();
            return writer;
        }
        //-----------------------------------------------------------------------------------------------
        public HtmlTextWriter PrintBtnUp(HtmlTextWriter writer, int id)
        {
            ClientScriptManager cs = Page.ClientScript;
            PostBackOptions opt = new PostBackOptions(this);
            opt.ActionUrl = Context.Request.RawUrl;
            opt.Argument = "moveUp," + id.ToString();

            writer.AddAttribute("onclick", cs.GetPostBackEventReference(opt));
            writer.AddAttribute("title", "Вверх");
            writer.AddAttribute("style", "cursor: pointer;");
            PrintImage(writer, "/Admin/img/btn_blue_up.jpg");
            return writer;
        }
        //-----------------------------------------------------------------------------------------------
        public HtmlTextWriter PrintBtnDown(HtmlTextWriter writer, int id)
        {
            ClientScriptManager cs = Page.ClientScript;
            PostBackOptions opt = new PostBackOptions(this);
            opt.ActionUrl = Context.Request.RawUrl;
            opt.Argument = "moveDown," + id.ToString();

            writer.AddAttribute("onclick", cs.GetPostBackEventReference(opt));
            writer.AddAttribute("title", "Вниз");
            writer.AddAttribute("style", "cursor: pointer;");
            PrintImage(writer, "/Admin/img/btn_blue_down.jpg");
            return writer;
        }
        //-----------------------------------------------------------------------------------------------
        public HtmlTextWriter PrintText(HtmlTextWriter writer, string Text, int cID)
        {
            string link = Context.Request.RawUrl;
            if (Context.Request.RawUrl.IndexOf("&catId=") != -1)
            {
                link = link.Substring(0, link.IndexOf("&catId="));
            }
            writer.AddAttribute(HtmlTextWriterAttribute.Href, link +"&catId=" + cID.ToString());
            writer.AddAttribute("style", "cursor: pointer;");
            writer.RenderBeginTag(HtmlTextWriterTag.A);
            writer.Write(Text + "!");
            writer.RenderEndTag();
            return writer;
        }
        //-----------------------------------------------------------------------------------------------
        public HtmlTextWriter PrintImage(HtmlTextWriter writer, string imgUrl)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Src, UrlHelper.GetSiteUrl() + imgUrl);
            writer.AddAttribute(HtmlTextWriterAttribute.Align, "absmiddle");
            writer.RenderBeginTag(HtmlTextWriterTag.Img);
            writer.RenderEndTag();
            return writer;
        }
        //-----------------------------------------------------------------------------------------------   
        
    }
}
